import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_application_1/components/load_more/load_more.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_application_1/components/lottie_refresh_header/lottie_refresh_header.dart';

import './components/banner.dart';
import './components/rank_row.dart';
import './components/info_item.dart';

/// 娃娃机
class TabBar1 extends StatefulWidget {
  final Color primaryColor;
  final Color bgColor;

  TabBar1({
    this.primaryColor = Colors.transparent,
    this.bgColor = Colors.transparent,
  });

  @override
  _TabBar1State createState() => _TabBar1State();
}

class _TabBar1State extends State<TabBar1> with AutomaticKeepAliveClientMixin {
  RefreshController _controller = RefreshController();

  // 下拉刷新
  Future _onRefresh() async {
    await Future.delayed(Duration(milliseconds: 1500));
    _controller.refreshCompleted();
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    return RefreshConfiguration(
      headerTriggerDistance: LottieRefreshHeader.size,
      child: SmartRefresher(
        controller: _controller,
        // 下拉刷新
        enablePullDown: true,
        onRefresh: _onRefresh,
        header: LottieRefreshHeader(),
        // 上拉加载
        enablePullUp: true,
        footer: CustomFooter(
          loadStyle: LoadStyle.HideAlways,
          builder: (_, __) => LoadMore(
            status: LoadMoreStatus.NoMore,
          ),
        ),
        child: CustomScrollView(
          slivers: [
            SliverToBoxAdapter(
              child: SizedBox(height: 5),
            ),
            // 轮播图
            SliverToBoxAdapter(
              child: ThatPageBanner(
                urls: [
                  'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=49081054,840221900&fm=224&gp=0.jpg',
                  'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201312%2F03%2F165526ophx4l6c6ll3cnpl.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1627202271&t=fac24391f44cb8882fc7399f77011b4b',
                ],
              ),
            ),
            // 榜单排名
            SliverToBoxAdapter(
              child: RankRow(
                primaryColor: widget.primaryColor,
                rank: [
                  'https://i2.hdslb.com/bfs/face/27956154ebc30ab1de758b6ad53daee844a2e42c.jpg@160w_160h_1c.jpg',
                  'https://i0.hdslb.com/bfs/face/fa7a39f18a4f89c48a37dc06bd450dac77b7242d.jpg@96w_96h_1c_1s.jpg',
                  'https://i2.hdslb.com/bfs/face/798a769a340e53ab71ea6b311606f4f46709f3f7.jpg@96w_96h_1c_1s.jpg',
                  'https://i2.hdslb.com/bfs/face/d6eedabf96628dc46c5b226dbdfadd3f303754b7.jpg@96w_96h_1c_1s.jpg',
                  'https://i0.hdslb.com/bfs/face/c87393e894b03864cfd5df4578e1178a212cb83d.jpg@96w_96h_1c_1s.jpg',
                ],
                onTap: () {
                  print('跳转到榜单排名');
                },
              ),
            ),
            // 列表
            SliverToBoxAdapter(
              child: Container(
                margin: EdgeInsets.only(
                  bottom: 10,
                  left: 10,
                  right: 10,
                ),
                padding: EdgeInsets.all(20),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Wrap(
                  spacing: 30,
                  runSpacing: 15,
                  children: List.generate(
                    20,
                    (index) => InfoItem(
                      primaryColor: widget.bgColor,
                      data: InfoItemData(
                        cover:
                            'https://file.amatech.cn/view/cee60e186cdc425397e4e96faf93d46b?spec=original',
                        name: '握紧的拳头',
                        price: (index + 20).toStringAsFixed(0),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
